package com.skinchange.attr;

import android.content.Context;
import android.util.AttributeSet;

import com.skinchange.config.Const;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Author: Author
 * Date: 2017-08-08
 * Time: 16:38
 */


public class SkinAttrSupport {

    private static SkinAttrType supportAttrType;

    public static List<SkinAttr> getSkinAttrs(AttributeSet attrs, Context context) {

        List<SkinAttr> mSkinAttrs = new ArrayList<>();
        SkinAttrType attrType = null;
        SkinAttr skinAttr = null;

        for (int i = 0, n = attrs.getAttributeCount(); i < n; i++) {
            String attrName = attrs.getAttributeName(i);
            String attrVal = attrs.getAttributeValue(i);

            if (attrVal.startsWith("@")) {
                int id = Integer.parseInt(attrVal.substring(1));
                String resName = context.getResources().getResourceEntryName(id);

                if (resName.startsWith(Const.SKIN_PREFIX)) {
                    attrType = getSupportAttrType(attrName);

                    if (attrType == null) break;
                    skinAttr = new SkinAttr(resName,attrType);
                    mSkinAttrs.add(skinAttr);
                }
            }
        }
        return mSkinAttrs;
    }

    public static SkinAttrType getSupportAttrType(String attrName) {

        for (SkinAttrType attrType:SkinAttrType.values()) {

            if (attrType.getResType().equals(attrName))

                return attrType;
        }
        return null;
    }
}
